using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    public class SubTemplateLoader : CompositeControl
    {
        private string templatePath;

        protected string TemplatePath
        {
            get { return templatePath; }
            set { templatePath = value; ChildControlsCreated = false;}
        }

        private string templateFileName;
        public string TemplateFileName
        {
            get { return templateFileName; }
            set { templateFileName = value; }
        }
	

        private BaseTemplateControl control;
        public BaseTemplateControl Control
        {
            get
            {
                EnsureChildControls();
                return control;
            }
        }

        protected override void CreateChildControls()
        {
            //if template path not set then try to load it from template
            if (string.IsNullOrEmpty(TemplatePath))
            {
                if (CurrentPost == null)
                {
                    CurrentPost = FindCurrentPost();
                }
                if (CurrentPost != null)
                {
                    Acms.Bll.TemplateFile tmp = null;
                    if (string.IsNullOrEmpty(TemplateFileName))
                    {
                        tmp = CurrentPost.GetDefaultFile();
                        if (tmp == null)
                        {
                            throw new Acms.Exceptions.TemplateException(string.Format("Default Template File could not be loaded for {0}. Path {1}", CurrentPost.TemplateName, CurrentPost.Path));
                        }
                    }
                    else
                    {
                        tmp = CurrentPost.GetFile(TemplateFileName);
                        if (tmp == null)
                        {
                            throw new Acms.Exceptions.TemplateException(string.Format("Template File '{2}' could not be loaded for {0}. Path {1}", CurrentPost.TemplateName, CurrentPost.Path, TemplateFileName));
                        }
                    }
                    templatePath = tmp.Path;
                }
            }
            if (!string.IsNullOrEmpty(TemplatePath))
            {
                Controls.Clear();
                try
                {
                    control = Page.LoadControl(TemplatePath) as BaseTemplateControl;
                }
                catch(System.Web.HttpException e)
                {
                    throw new Acms.Exceptions.TemplateException(string.Format("Template with path '{0}' could not be loaded ({1})", TemplatePath, e.Message), e);
                }
                if (control != null)
                {
                    this.Controls.Add(control);
                    Control.CurrentPost = this.CurrentPost; 
                    Control.HighlightText = this.HighlightText;
                }
                else
                {
                    throw new Acms.Exceptions.TemplateException(string.Format("Template with path '{0}' did not inherit from Acms.AcmsControls.BaseTemplateControl", TemplatePath));
                }
            }
            base.CreateChildControls();
        }

        private Acms.Bll.Post currentPost;
        public Acms.Bll.Post CurrentPost
        {
            get { return currentPost; }
            set { currentPost = value; templatePath = null; this.ChildControlsCreated = false; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            EnsureChildControls();
            base.OnPreRender(e);
        }

        public string HighlightText
        {
            get { return ViewState["HighlightText"] as string; }
            set { ViewState["HighlightText"] = value; }
        }

        private Acms.Bll.Post FindCurrentPost()
        {
            IDataItemContainer container = null;
            Control current = this;
            while ((container == null || !(container.DataItem is Acms.Bll.Post)) && current != null && current.NamingContainer != null)
            {
                current = current.NamingContainer;
                container = current as IDataItemContainer;
            }
            if (container != null)
            {
                return container.DataItem as Acms.Bll.Post;
            }
            else
            {
                return null;
            }
        }

        protected override object SaveViewState()
        {
            ViewState["TemplatePath"] = templatePath;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            templatePath = ViewState["TemplatePath"] as string;
        }
    }
}
